package com.shanwen.select.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shanwen.select.entity.*;
import com.shanwen.select.service.IOrderService;
import com.shanwen.select.service.IPriceService;
import com.shanwen.select.service.WeixinUserInfoSerivice;
import com.shanwen.select.utils.UtilTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 订单数据
 */
@Controller
@RequestMapping("/order")
public class OrderController {


    @Autowired
    IOrderService orderService;

    @Autowired
    IPriceService priceService;


    @Autowired
    WeixinUserInfoSerivice weixinUserInfoSerivice;


    @RequestMapping("/wxuser")
    public String userList(Model model, String phone, Integer pageNum, Integer pageSize) {
        if (pageSize == null) {
            pageSize = 10;
        }
        QueryWrapper<WeixinUserInfo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(phone)) {
            queryWrapper.eq("phone", phone);
        }
        queryWrapper.orderByDesc("create_date");
        PageHelper.startPage(pageNum == null ? 1 : pageNum, pageSize);
        PageInfo<WeixinUserInfo> pageInfo = new PageInfo<>(weixinUserInfoSerivice.list(queryWrapper));

        model.addAttribute("phone", phone);
        model.addAttribute("wxuserList", pageInfo.getList());
        model.addAttribute("pageInfo", pageInfo);

        return "order/wxuser";
    }

    @RequestMapping(value = "/userUpdate", method = RequestMethod.GET)
    public String userUpdate(String userId, String phone, String pageNum) {

        WeixinUserInfo weixinUserInfo = weixinUserInfoSerivice.getById(userId);
        //设置开始时间
        weixinUserInfo.setRegisterDate(new Date());
        //设置过期时间
        QueryWrapper<Price> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_Date");
        List<Price> priceList = priceService.list(queryWrapper);

        weixinUserInfo.setExpireDate(UtilTool.rollDay(new Date(), priceList.get(0).getDuration()));
        weixinUserInfo.setLevel(1);
        weixinUserInfoSerivice.updateUserInfo(weixinUserInfo);
        return "redirect:../order/wxuser?phone="+phone+"&pageNum="+pageNum;
    }


    /**
     * 订单数据列表
     */

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String orderList(Model model) {

        Map map = new HashMap<>();

        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, +1);
        String startDate = sdf.format(calendar.getTime());

        map.put("startDate", startDate);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        String endDate = sdf.format(calendar.getTime());
        map.put("endDate", endDate);

        Map dailyResult = orderService.getOrderData(map);
        calendar.setTime(date);
        calendar.add(Calendar.WEEK_OF_MONTH, -1);
        endDate = sdf.format(calendar.getTime());
        map.put("endDate", endDate);

        Map weekResult = orderService.getOrderData(map);
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -1);
        endDate = sdf.format(calendar.getTime());
        map.put("endDate", endDate);
        Map monthResult = orderService.getOrderData(map);
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, -1);
        endDate = sdf.format(calendar.getTime());
        map.put("endDate", endDate);
        Map yearResult = orderService.getOrderData(map);


        model.addAttribute("dailyResult", dailyResult);
        model.addAttribute("weekResult", weekResult);
        model.addAttribute("monthResult", monthResult);
        model.addAttribute("yearResult", yearResult);

        return "order/list";
    }

    /**
     * 当前价格
     */

    @RequestMapping(value = "/price", method = RequestMethod.GET)
    public String getPrice(Model model) {


        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("create_Date");
        List<Price> priceList = priceService.list(queryWrapper);
        model.addAttribute("priceList", priceList);
        return "order/price";
    }

    /**
     * 订单列表
     */

    @RequestMapping(value = "/orderDetail", method = RequestMethod.GET)
    public String detailGet(Model model, String phone, String ordernum, Integer pageNum, Integer pageSize) {
        if (null == pageSize) {
            pageSize = 10;
        }

        Map map = new HashMap();
        map.put("phone", phone);
        map.put("ordernum", ordernum);

        PageHelper.startPage(pageNum == null ? 1 : pageNum, pageSize);
        PageInfo pageInfo = new PageInfo(orderService.getOrderList(map));

        model.addAttribute("ordernum", ordernum);
        model.addAttribute("phone", phone);
        model.addAttribute("orderList", pageInfo.getList());
        model.addAttribute("pageInfo", pageInfo);
        return "order/orderDetail";
    }

    @RequestMapping(value = "/orderDetail", method = RequestMethod.POST)
    public String detailPost(Model model, String phone, String ordernum, Integer pageNum, Integer pageSize) {
        if (null == pageSize) {
            pageSize = 10;
        }

        Map map = new HashMap();
        map.put("phone", phone);
        map.put("ordernum", ordernum);

        PageHelper.startPage(pageNum == null ? 1 : pageNum, pageSize);
        PageInfo pageInfo = new PageInfo(orderService.getOrderList(map));

        model.addAttribute("ordernum", ordernum);
        model.addAttribute("phone", phone);
        model.addAttribute("orderList", pageInfo.getList());
        model.addAttribute("pageInfo", pageInfo);
        return "order/orderDetail";
    }
}
